<!--
 * @Descripttion: (房型调价/lay_room_adjust)
 * @Author: (admin)
 * @Date: (2024-11-01)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['layroomadjust:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['layroomadjust:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['layroomadjust:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <!-- <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('remark')" /> -->

      <!-- <el-table-column prop="isHoliday" label="节假日" align="center" v-if="columns.showColumn('isHoliday')">
        <template #default="scope">
          <dict-tag :options="options.isHolidayOptions" :value="scope.row.isHoliday" />
        </template>
</el-table-column>
<el-table-column prop="isWeek" label="周末" align="center" v-if="columns.showColumn('isWeek')">
  <template #default="scope">
          <dict-tag :options="options.isWeekOptions" :value="scope.row.isWeek" />
        </template>
</el-table-column>
<el-table-column prop="isDaily" label="日常" align="center" v-if="columns.showColumn('isDaily')">
  <template #default="scope">
          <dict-tag :options="options.isDailyOptions" :value="scope.row.isDaily" />
        </template>
</el-table-column> -->
      <!-- <el-table-column prop="id" label="Id" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('id')" /> -->
      <el-table-column prop="name" label="调价名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('name')" />
      <el-table-column prop="startDate" label="开始日期" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('startDate')">
        <template #default="scope">
          {{ dayjs(scope.row.startDate).format("YYYY-MM-DD") }}
        </template></el-table-column>
      <el-table-column prop="endDate" label="结束日期" :show-overflow-tooltip="true" align="center"
        v-if="columns.showColumn('endDate')">
        <template #default="scope">
          {{ dayjs(scope.row.endDate).format("YYYY-MM-DD") }}
        </template>
      </el-table-column>
      <el-table-column prop="roomNames" label="房型名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('roomNames')" />

      <el-table-column prop="typeName" label="调价类型" align="center" v-if="columns.showColumn('typeId')" />

      <el-table-column prop="adjustTypeName" label="金额类型" align="center" v-if="columns.showColumn('adjustType')">
      </el-table-column>

      <el-table-column prop="price" :label="form.typeId == 1 ? '金额' : '比例'" align="center"
        v-if="columns.showColumn('price')">

        <template #default="scope">
          {{ scope.row.price }} {{ form.typeId == 1 ? '元' : '%' }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="roomIds" label="房型" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('roomIds')" /> -->


      <el-table-column prop="title" label="调价说明" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('title')" />

      <el-table-column prop="status" label="状态" align="center" v-if="columns.showColumn('status')">
        <template #default="scope">
          <dict-tag :options="options.status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['layroomadjust:edit']"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['layroomadjust:delete']"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <!-- <el-col :lg="12">
            <el-form-item label="是否全部" prop="isAll">
              <el-radio-group v-model="form.isAll">
                <el-radio v-for="item in options.isAllOptions" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="更新人" prop="updateBy">
              <el-input v-model="form.updateBy" placeholder="请输入更新人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-date-picker v-model="form.updateTime" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建人" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker v-model="form.createTime" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col> -->






          <!-- <el-col :lg="12">
            <el-form-item label="Id" prop="id">
              <el-input v-model="form.id" placeholder="请输入Id" :disabled="opertype != 1" />
            </el-form-item>
          </el-col> -->



          <!-- <el-col :lg="24">
            <el-form-item label="房型名称" prop="roomNames">
              <el-input type="textarea" v-model="form.roomNames" placeholder="请输入房型名称" />
            </el-form-item>
          </el-col> -->
          <el-col :lg="16">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status">
                <el-option v-for="item in options.status" :key="item.dictValue" :value="item.dictValue"
                  :label="item.dictLabel">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="24">
            <el-form-item label="调价说明" prop="title">
              <el-input v-model="form.title" placeholder="请输入调价说明" />
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="房型" prop="roomIds">
              <el-cascader placeholder="搜索或选择" :options="dataTreeList" :size="medium" :props="{ emitPath: false }"
                filterable v-model="form.roomIds"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker v-model="form.startDate" type="date" placeholder="选择日期时间" value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="开始日期" prop="endDate">
              <el-date-picker v-model="form.endDate" type="date" placeholder="选择日期时间" value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>


          <el-col :lg="8">
            <el-form-item label="调价类型" prop="adjustType">
              <el-select v-model="form.adjustType" placeholder="请选择节假日">
                <el-option v-for="item in options.AdjustTypeEnum" :key="item.key" :label="item.value"
                  :value="parseInt(item.key)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="金额类型" prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择金额类型">
                <el-option v-for="item in options.AdjustPriceTypeEnum" :key="item.key" :label="item.value"
                  :value="parseInt(item.key)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item :label="form.typeId == 1 ? '金额' : '比例'" prop="price">
              <el-input v-model="form.price" type="number" min=1 placeholder="请输入">
                <template #suffix>{{ form.typeId == 1 ? '元' : '%' }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="5">
            <el-form-item label="" prop="isHoliday">
              <el-checkbox v-model="form.isHoliday">法定节假日
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :lg="5">
            <el-form-item label="" prop="isDaily">
              <el-checkbox v-model="form.isDaily">工作日
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :lg="5">
            <el-form-item label="" prop="isWeek">
              <el-checkbox v-model="form.isWeek">周末
              </el-checkbox>
            </el-form-item>
          </el-col>



          <el-col :lg="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="layroomadjust">
import {
  listLayRoomAdjust,
  addLayRoomAdjust, delLayRoomAdjust,
  updateLayRoomAdjust, getLayRoomAdjust,
}
  from '@/api/business/layroomadjust.js'

import { AdjustTypeEnum, AdjustPriceTypeEnum } from '@/api/business/layEnum.js'
import { GetTreeLayHouseRoom } from '@/api/business/layhouse.js'
import { dayjs, formatter } from 'element-plus';
import { min } from 'xe-utils';
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'a.CreateTime',
  sortType: 'desc',
  name: undefined,
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'remark', label: '备注', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'dict', prop: 'status', label: '状态', showOverflowTooltip: true },
  { visible: true, align: 'center', type: 'dict', prop: 'isHoliday', label: '节假日' },
  { visible: false, align: 'center', type: 'dict', prop: 'isWeek', label: '周末' },
  { visible: false, align: 'center', type: 'dict', prop: 'isDaily', label: '日常' },
  { visible: false, align: 'center', type: '', prop: 'id', label: 'Id', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'price', label: '金额' },
  { visible: true, align: 'center', type: 'dict', prop: 'adjustType', label: '金额类型' },
  { visible: true, align: 'center', type: '', prop: 'typeId', label: '调价类型' },
  { visible: false, align: 'center', type: '', prop: 'roomIds', label: '房型', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'roomNames', label: '房型名称', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'endDate', label: '结束日期', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'startDate', label: '开始日期', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'title', label: '调价说明', showOverflowTooltip: true },
  { visible: true, align: 'center', type: '', prop: 'name', label: '名称', showOverflowTooltip: true },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const dataTreeList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
  "status",
]

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})
AdjustTypeEnum().then(res => {
  const { code, data } = res
  if (code == 200) {
    state.options.AdjustTypeEnum = data
  }
})
AdjustPriceTypeEnum().then(res => {
  const { code, data } = res
  if (code == 200) {
    state.options.AdjustPriceTypeEnum = data
  }
})

GetTreeLayHouseRoom().then(res => {
  const { code, data } = res
  if (code == 200) {
    dataTreeList.value = data
  }

})
function getList() {
  loading.value = true
  listLayRoomAdjust(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1
  multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    name: [{ required: true, message: "调价名称不能为空", trigger: "blur" }],
    startDate: [{ required: true, message: "开始日期不能为空", trigger: "blur" }],
    endDate: [{ required: true, message: "结束日期不能为空", trigger: "blur" }],
    roomIds: [{ required: true, message: "房型不能为空", trigger: "blur" }],
    typeId: [{ required: true, message: "调价类型不能为空", trigger: "blur" }],
    adjustType: [{ required: true, message: "金额类型不能为空", trigger: "blur" }],
    price: [{ required: true, message: "额度不能为空", trigger: "blur" }],
  },
  options: {
    // 是否全部 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    isAllOptions: [],
    status: [],
    AdjustTypeEnum: [],
    AdjustPriceTypeEnum: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    isAll: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    createTime: null,
    remark: null,
    status: null,
    isHoliday: false,
    isWeek: false,
    isDaily: false,
    id: null,
    price: null,
    adjustType: null,
    typeId: null,
    roomIds: null,
    roomNames: null,
    endDate: null,
    startDate: null,
    title: null,
    name: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  form.value.status = '1'
  form.value.adjustType = 1,
    form.value.typeId = 1
  title.value = '房型调价'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getLayRoomAdjust(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改房型调价'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      form.value.startDate = dayjs(form.value.startDate, Date).format("YYYY-MM-DD 00:00:00")
      form.value.endDate = dayjs(form.value.endDate, Date).format("YYYY-MM-DD 00:00:00")
      if (form.value.id != undefined && opertype.value === 2) {
        updateLayRoomAdjust(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLayRoomAdjust(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLayRoomAdjust(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}




handleQuery()
</script>